package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Coursetype;
import com.why.entity.Kechengxinxi;
import com.why.service.KechengxinxiService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程信息(Kechengxinxi)表控制层
 *
 * @author makejava
 * @since 2023-05-11 13:52:02
 */
@RestController
@RequestMapping("/kechengxinxi")
public class KechengxinxiController {
    /**
     * 服务对象
     */
    @Autowired
    private KechengxinxiService kechengxinxiService;


    @GetMapping("/getcourse")
    public List list() {
        LambdaQueryWrapper<Kechengxinxi> queryWrapper = new LambdaQueryWrapper<Kechengxinxi>();
        queryWrapper.eq(Kechengxinxi::getStatus, 1);
        List<Kechengxinxi> kechengxinxis = kechengxinxiService.list(queryWrapper);
        return kechengxinxis;
    }

    @PostMapping("/save")
    public String save(@RequestBody Kechengxinxi kechengxinxi) {
        kechengxinxiService.save(kechengxinxi);
        return "success";
    }

    @GetMapping("/page")
    public Page<Kechengxinxi> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Kechengxinxi> queryWrapper = new LambdaQueryWrapper<Kechengxinxi>();
        queryWrapper.like(name != null, Kechengxinxi::getKechengmingcheng, name);
        queryWrapper.orderByDesc(Kechengxinxi::getAddtime);
        kechengxinxiService.page(pageInfo, queryWrapper);
        return pageInfo;
    }


    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        LambdaQueryWrapper<Kechengxinxi> queryWrapper = new LambdaQueryWrapper<Kechengxinxi>();
        queryWrapper.eq(Kechengxinxi::getId, id);
        if (kechengxinxiService.getOne(queryWrapper) != null) {
            kechengxinxiService.removeById(id);
            return "success";
        } else {
            return "error";
        }
    }


    @PostMapping("/update")
    public String update(@RequestBody Kechengxinxi kechengxinxi) {
        LambdaQueryWrapper<Kechengxinxi> queryWrapper = new LambdaQueryWrapper<Kechengxinxi>();
        queryWrapper.eq(Kechengxinxi::getId, kechengxinxi.getId());
        Kechengxinxi kechengxinxiOne = kechengxinxiService.getOne(queryWrapper);
        System.out.println(kechengxinxi);
        if (kechengxinxiOne != null) {
            kechengxinxiService.update(kechengxinxi, queryWrapper);
            return "success";
        } else {
            return "error";
        }
    }
}

